<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 15/11/22
 * Time: 下午5:05
 */
class ApiController extends Controller {

    public function index(){
        $query  = $_REQUEST;
        $sign   = $query['sign'];

        unset($query['r']);
        unset($query['sign']);

        $query['key'] = ModApp::model()->getAppKey($query['app']);

        ksort($query);

        if(!empty($query['key']) && $sign === md5(http_build_query($query))){
            $methods = explode(',', $query['method']);

            $data   = array();
            $cnt    = count($methods);
            // 遍历方法获取数据
            foreach($methods as $v){
                $_m = trim($v);

                if(strpos($_m, '::') > 0){
                    list($_c, $_a) = explode('::', $_m);
                }else{
                    $_c = $_m;
                    $_a = 'get';
                }

                $result = call_user_func(array('Api'.(ucfirst($_c)), $_a), $query);
                if($cnt > 1){
                    $data[$_m] = $result;
                }else{
                    $data = $result;
                }
            }
            $this->out = $data;

        }else{
            $this->out = null;
        }

        $this->outType='json';
    }


    public function getServer(){
        $this->outType = 'json';

        $platform = $this->R('p');
        $game     = $this->R('g');

        $options  = array('ALL' => 'ALL');
        $server = PubBase::getServer($platform, $game);
        $this->out = array_merge($options, $server);
    }

    public function getGame(){
        $p = $_REQUEST['p'];
        //ACC存的是r2games，需要转换一次
        if($p == 'en'){
            $p = 'r2games';
        }
        $this->outType = 'json';
        $this->out = array_merge(PubGame::getGameForSel($p));
    }

}